package com.morehair.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.morehair.bean.bo.admin.*;
import com.morehair.bean.pojo.*;
import com.morehair.bean.vo.admin.*;
import com.morehair.mapper.*;
import com.morehair.service.admin.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author raohui
 * @Date 2021/6/4 22:18
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    FeedbackMapper feedbackMapper;
    @Autowired
    HistoryMapper historyMapper;

    @Override
    public UserDataVo selectUserData(UserBo user) {
        //在查询之前使用分页插件，会在执行过程中拼接sql语句
        PageHelper.startPage(user.getPage(),user.getLimit());
        //每一个service都创建一个新的example对象
        UserExample userExample = new UserExample();
        //排序信息
        userExample.setOrderByClause(user.getSort()+" "+user.getOrder());
        UserExample.Criteria criteria=userExample.createCriteria();
        if (user.getUsername() != null && !"".equals(user.getUsername())) {
            criteria.andUsernameLike("%"+user.getUsername()+"%");
        }
        if (user.getMobile() != null && !"".equals(user.getMobile())) {
            criteria.andMobileLike("%"+user.getMobile()+"%");
        }

        List<User> users = userMapper.selectByExample(userExample);
        //获取分页信息
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        long total = userPageInfo.getTotal();

        UserDataVo userDataVo = new UserDataVo();
        userDataVo.setTotal((int) total);
        userDataVo.setItems(users);
        return userDataVo;
    }

    @Override
    public AddressDataVo selectAddressData(AddressBo user) {
        //在查询之前使用分页插件，会在执行过程中拼接sql语句
        PageHelper.startPage(user.getPage(),user.getLimit());
        //每一个service都创建一个新的example对象
        AddressExample addressExample = new AddressExample();
        //排序信息
        addressExample.setOrderByClause(user.getSort()+" "+user.getOrder());


        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (user.getUserId() != null && !"".equals(user.getUserId())) {
            criteria.andUserIdEqualTo(user.getUserId());
        }
        if (user.getName() != null && !"".equals(user.getName())) {
            criteria.andNameLike("%" + user.getName() + "%");
        }

        List<Address> addresses = addressMapper.selectByExample(addressExample);

        //获取分页信息
        PageInfo<Address> addressPageInfo = new PageInfo<>(addresses);
        long total = addressPageInfo.getTotal();
        AddressDataVo addressDataVo = new AddressDataVo();
        addressDataVo.setTotal((int) total);
        addressDataVo.setItems(addresses);
        return addressDataVo;
    }

    @Override
    public CollectDataVo selectCollectData(CollectBo user) {
        //在查询之前使用分页插件，会在执行过程中拼接sql语句
        PageHelper.startPage(user.getPage(),user.getLimit());
        //每一个service都创建一个新的example对象
        CollectExample collectExample = new CollectExample();
        //排序信息
        collectExample.setOrderByClause(user.getSort()+" "+user.getOrder());

        CollectExample.Criteria criteria = collectExample.createCriteria();
        if (user.getUserId() != null && !"".equals(user.getUserId())) {
            criteria.andUserIdEqualTo(user.getUserId());
        }
        if (user.getValueId() != null && !"".equals(user.getValueId())) {
            criteria.andValueIdEqualTo(user.getValueId());
        }
        List<Collect> collects = collectMapper.selectByExample(collectExample);

        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
        long total = collectPageInfo.getTotal();
        CollectDataVo collectDataVo = new CollectDataVo();
        collectDataVo.setTotal((int) total);
        collectDataVo.setItems(collects);
        return collectDataVo;
    }

    @Override
    public FootprintDataVo selectFootprintData(FootprintBo user) {
        PageHelper.startPage(user.getPage(), user.getLimit());
        FootprintExample footprintExample = new FootprintExample();
        footprintExample.setOrderByClause(user.getSort()+" "+user.getOrder());
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        if (user.getUserId() != null && !"".equals(user.getUserId())) {
            criteria.andUserIdEqualTo(user.getUserId());
        }
        if(user.getGoodsId()!=null&&!"".equals(user.getGoodsId())){
            criteria.andGoodsIdEqualTo(user.getGoodsId());
        }
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
        long total = footprintPageInfo.getTotal();
        FootprintDataVo footprintDataVo = new FootprintDataVo();
        footprintDataVo.setTotal((int) total);
        footprintDataVo.setItems(footprints);
        return footprintDataVo;
    }

    @Override
    public FeedbackDataVo selectFeedbackData(FeedbackBo user) {
        PageHelper.startPage(user.getPage(), user.getLimit());
        FeedbackExample feedbackExample = new FeedbackExample();
        feedbackExample.setOrderByClause(user.getSort() + " " + user.getOrder());
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if (user.getUsername() != null && !"".equals(user.getUsername())) {
            criteria.andUsernameLike("%" + user.getUsername() + "%");
        }
        if (user.getFeedbackId() != null && !"".equals(user.getFeedbackId())) {
            criteria.andIdEqualTo(user.getFeedbackId());
        }
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        PageInfo<Feedback> feedbackPageInfo = new PageInfo<>(feedbacks);
        long total = feedbackPageInfo.getTotal();
        FeedbackDataVo feedbackDataVo = new FeedbackDataVo();
        feedbackDataVo.setTotal((int) total);
        feedbackDataVo.setItems(feedbacks);
        return feedbackDataVo;
    }

    @Override
    public HistoryDataVo selectHistoryData(HistoryBo user) {
        PageHelper.startPage(user.getPage(), user.getLimit());
        HistoryExample historyExample = new HistoryExample();
        historyExample.setOrderByClause(user.getSort() + " " + user.getOrder());
        HistoryExample.Criteria criteria = historyExample.createCriteria();
        if (user.getKeyword() != null && !"".equals(user.getKeyword())) {
            criteria.andKeywordLike("%" + user.getKeyword() + "%");
        }
        if (user.getUserId() != null ) {
            criteria.andIdEqualTo(user.getUserId());
        }
        List<History> histories = historyMapper.selectByExample(historyExample);
        PageInfo<History> historyPageInfo = new PageInfo<>(histories);
        long total = historyPageInfo.getTotal();
        HistoryDataVo historyDataVo = new HistoryDataVo();
        historyDataVo.setTotal((int) total);
        historyDataVo.setItems(histories);
        return historyDataVo;
    }


}
